package com.example.tablistfractionzlw1002.slice;

import com.example.tablistfractionzlw1002.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.TabList;

public class MainAbilitySlice extends AbilitySlice {
    String[] s={"首页","视频","我的"};
    TabList tabList;
    TabList.Tab tab;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        tabList= (TabList) findComponentById(ResourceTable.Id_tablist);
        for (int i=0;i<s.length;i++){
            tab=tabList.new Tab(this);
            tab.setText(s[i]);
            tabList.addTab(tab);
            if (s[i].equals("首页")){
                tabList.selectTab(tab);
                pos(0);
            }
        }
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                int position = tab.getPosition();
                pos(position);
            }

            @Override
            public void onUnselected(TabList.Tab tab) {

            }

            @Override
            public void onReselected(TabList.Tab tab) {

            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }
    public  void  pos(int position){
        switch (position){
            case 0:
                ((FractionAbility)getAbility())
                        .getFractionManager()
                        .startFractionScheduler().
                        replace(ResourceTable.Id_direct,new Fration_one()).submit();
                break;
            case 1:
                ((FractionAbility)getAbility())
                        .getFractionManager().
                        startFractionScheduler().
                        replace(ResourceTable.Id_direct,new Fration_move()).submit();
                break;
            case 2:
                ((FractionAbility)getAbility())
                        .getFractionManager().startFractionScheduler().
                        replace(ResourceTable.Id_direct,new Fratino_me()).submit();
                break;
            default:
                break;
        }
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
